package com.qf.web.controller;

import com.qf.domain.Cart;
import com.qf.domain.Goods;
import com.qf.domain.User;
import com.qf.service.CartService;
import com.qf.service.GoodsService;
import com.qf.service.impl.CartServiceImpl;
import com.qf.service.impl.GoodsServiceImpl;
import com.qf.utils.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;

/**
 * projectName:xiaomi1
 * author:
 * time:2024/4/17 11:02
 * description:
 */
@WebServlet("/cartservlet")
//http://localhost:8080/goodsservlet?method=getGoodsListByTypeId&typeId=1#/
public class CartServlet extends BaseServlet {
    public String addCart(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User user = (User) request.getSession().getAttribute("user");
        if (user==null){
            return "redirect:/login.jsp";
        }
        String goodsId = request.getParameter("goodsId");
        String number = request.getParameter("number");
        if (StringUtils.isEmpty(goodsId)){
            request.setAttribute("msg","商品id不能为空");
            return "/message.jsp";
        }
        int num=1;
        if (!StringUtils.isEmpty(number)){
            try {
                num=Integer.parseInt(number);
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
        }
        try {
            CartService cartService = new CartServiceImpl();
            int good_id = Integer.parseInt(goodsId);
            GoodsService goodsService = new GoodsServiceImpl();
            Goods goods = goodsService.findById(good_id);
            Cart cart = cartService.find(user.getId(),good_id);
            if (cart!=null){
                cart.setNum(cart.getNum()+num);
                cart.setMoney(goods.getPrice().multiply(new BigDecimal(cart.getNum())));
                cartService.modify(cart);
            }else {
                cart = new Cart(user.getId(),good_id,num,goods.getPrice().multiply(new BigDecimal(num)));
                cartService.add(cart);
            }
            return "redirect:/cartSuccess.jsp";
        } catch (NumberFormatException e) {
            e.printStackTrace();
            request.setAttribute("msg","添加购物车失败"+e.getMessage());
            return "/message.jsp";
        }
    }
    public String getCart(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1判断用户是否登录
        User user = (User) request.getSession().getAttribute("user");
        if (user == null) {
            return "redirect:/login.jsp";
        }
        //调用业务逻辑
        try {
            CartService cartService = new CartServiceImpl();
            List<Cart> cartList = cartService.find(user.getId());
            //转发重定向
            request.setAttribute("cartList", cartList);
            return "/cart.jsp";
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msg", "查看购物车失败：" + e.getMessage());
            return "/message.jsp";
        }
    }

    public String addCartAjax(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User user =(User)request.getSession().getAttribute("user");
        if (user==null){
            return "redirect:/login.jsp";
        }
        String goodsId = request.getParameter("goodsId");
        String number = request.getParameter("number");
        if (StringUtils.isEmpty(goodsId)){
            request.setAttribute("msg","商品id不能为空");
            return "/message.jsp";
        }

        try {
            int goods_id = Integer.parseInt(goodsId);
            int num = Integer.parseInt(number);
            CartService cartService = new CartServiceImpl();
            if (num==0){
                cartService.remove(user.getId(),goods_id);
            }else  if (num==1||num==-1){
                Cart cart = cartService.find(user.getId(),goods_id);
                if (cart!=null){
                    GoodsService goodsService = new GoodsServiceImpl();
                    Goods goods = goodsService.findById(goods_id);
                    cart.setMoney(goods.getPrice().multiply(new BigDecimal(cart.getNum())));
                    cartService.modify(cart);
                }
            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
            request.setAttribute("msg","购物车失败：" +e.getMessage());
            return "/message.jsp";
        }
        return null;
    }

    public String clearCartAjax(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1判断用户是否登录
        User user = (User) request.getSession().getAttribute("user");
        if (user == null) {
            return "redirect:/login.jsp";
        }
        try {
            //调用业务逻辑
            CartService cartService=new CartServiceImpl();
            cartService.clear(user.getId());
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msg", "清空购物车失败：" + e.getMessage());
            return "/message.jsp";
        }
        return null;
    }
}


